package com.ch.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class FormatUtils {
	/**
	 * <b>boolean轉字串</b>
	 * @param orgBoolean	欲轉換的數值
	 * @return
	 * @throws ParseException
	 */
	public static String booleanToString(boolean orgBoolean) throws ParseException {
		return String.valueOf(orgBoolean);
	}
	
	/**
	 * <b>char轉字串</b>
	 * @param orgChar	欲轉換的數值
	 * @return
	 * @throws ParseException
	 */
	public static String charToString(char orgChar) throws ParseException {
		return String.valueOf(orgChar);
	}
	
	/**
	 * <b>char陣列轉字串</b>
	 * @param orgCharArr	欲轉換的char陣列
	 * @return
	 * @throws ParseException
	 */
	public static String charArrayToString(char[] orgCharArr) throws ParseException {
		return String.valueOf(orgCharArr);
	}
	
	/**
	 * <b>字串轉Byte</b>
	 * @param orgStr	欲轉換的字串
	 * @return
	 * @throws ParseException
	 */
	public static Byte stringToByte(String orgStr) throws ParseException {
		return Byte.parseByte(orgStr);
	}
	
	/**
	 * <b>字串轉Double</b>
	 * @param orgStr	欲轉換的字串
	 * @return
	 * @throws ParseException
	 */
	public static Double stringToDouble(String orgStr) throws ParseException {
		return Double.parseDouble(orgStr);
	}
	
	/**
	 * <b>字串轉Float</b>
	 * @param orgStr	欲轉換的字串
	 * @return
	 * @throws ParseException
	 */
	public static float stringToFloat(String orgStr) throws ParseException {
		return Float.parseFloat(orgStr);
	}
	
	/**
	 * <b>字串轉Int</b>
	 * @param orgStr	欲轉換的字串
	 * @return
	 * @throws ParseException
	 */
	public static int stringToInt(String orgStr) throws ParseException {
		return Integer.parseInt(orgStr);
	}
	
	/**
	 * <b>字串轉long</b>
	 * @param orgStr	欲轉換的字串
	 * @return
	 * @throws ParseException
	 */
	public static long stringToLong(String orgStr) throws ParseException {
		return Long.parseLong(orgStr);
	}
	
	/**
	 * <b>字串轉Date</b>
	 * @param	orgStr				欲轉換的日期字串
	 * @param	simpleDateFormat	設定日期格式	
	 * @return
	 * @throws	ParseException
	 */
	public static Date stringToDate(String orgStr, String simpleDateFormat) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormat);
		return sdf.parse(orgStr);
	}
	
	/**
	 * <b>JSONObject轉字串</b>
	 * @param orgJSONObject	欲轉換的JSONObject
	 * @return
	 * @throws ParseException
	 */
	public static String JSONObjectToString(JSONObject orgJSONObject) throws ParseException {
		return orgJSONObject.toString();
	}
	
	/**
	 * <b>Object轉JSONObject</b>
	 * @param orgStr	欲轉換的字串
	 * @return
	 * @throws ParseException
	 */
	public static JSONObject objectToJSONObject(Object obj) throws ParseException {
		return JSONObject.fromObject(obj);
	}
	
	/**
	 * <b>Object轉JSONArray</b>
	 * @param orgStr	欲轉換的字串
	 * @return
	 * @throws ParseException
	 */
	public static JSONArray objectToJSONArray(Object obj) throws ParseException {
		return JSONArray.fromObject(obj);
	}
	
}
